package com.xxyy.mylive.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xxyy.mylive.R;

import java.util.List;

/**
 * Created by ado on 0:14 2016/10/19.
 */
public class HomeListAdapter extends RecyclerView.Adapter<HomeListAdapter.HomeHolder> {
    private Context context;
    private List list;
    private OnItemClickListener onItemClickListener;

    public final int VIDEO = 1001;//视频
    public final int VIDEO_VR = 1002;//VR视频
    public final int LIVE = 1003;//直播


    public HomeListAdapter(Context context, List list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getItemCount() {

        return list.size();
    }

    @Override
    public int getItemViewType(int position) {
        int type = 0;
        switch (position) {
            case 0:
                type = VIDEO;
                break;
            case 1:
                type = VIDEO_VR;
                break;
            default:
                type = LIVE;
        }
        return type;
    }

    @Override
    public HomeHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        switch (viewType) {
            case VIDEO:
                view = LayoutInflater.from(context).inflate(R.layout.home_list_item_video, parent, false);
                break;
            case VIDEO_VR:
                view = LayoutInflater.from(context).inflate(R.layout.home_liset_item_video_vr, parent, false);
                break;
            case LIVE:
                view = LayoutInflater.from(context).inflate(R.layout.home_list_item_live, parent, false);
                break;
        }
        return new HomeHolder(view, viewType);
    }

    @Override
    public void onBindViewHolder(HomeHolder holder, final int position) {
        //根据不同类型播放不同的视频
        holder.rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onItemClickListener.onItemclick(view,getItemViewType(position));
            }
        });
    }

    protected class HomeHolder extends RecyclerView.ViewHolder {
        private View rootView;
        private CheckBox cbFollow;
        private TextView tvBuy, tvName;

        public HomeHolder(View rootView, int type) {
            super(rootView);
            this.rootView = rootView;
            switch (type) {
                case VIDEO:
                    cbFollow = (CheckBox) rootView.findViewById(R.id.cb_video_follow);
                    break;
                case VIDEO_VR:
                    tvBuy = (TextView) rootView.findViewById(R.id.tv_buy);
                    break;
                case LIVE:
                    tvName = (TextView) rootView.findViewById(R.id.tv_user_name);
                    break;
            }
        }
    }

    public interface OnItemClickListener {
        void onItemclick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}
